
import 'dart:ui';

import 'package:moonplay/provide/fontsize_provide.dart';
/**
 * 封装文字小部件
 * 主要作用是跟随系统主题变化文字颜色和字体大小
 * 这里用到了main里面的默认文字颜色Theme.of(context).primaryColor
 */
import 'package:flutter/material.dart';
import 'package:moonplay/common/common_size.dart';
import 'package:moonplay/provide/provide.dart';

class Cmtext extends StatelessWidget {
  final String data;
  final String text;
  final TextStyle style;
  final StrutStyle strutStyle;
  final TextAlign textAlign;
  final TextDirection textDirection;
  final Locale locale;
  final bool softWrap;
  final TextOverflow overflow;
  final double textScaleFactor;
  final int maxLines;
  final String semanticsLabel;
  final TextWidthBasis textWidthBasis;

  const Cmtext(this.data,{
    Key key, 
    this.text, 
    this.style, 
    this.strutStyle, 
    this.textAlign, 
    this.textDirection, 
    this.locale, 
    this.softWrap, 
    this.overflow, 
    this.textScaleFactor, 
    this.maxLines, 
    this.semanticsLabel, 
    this.textWidthBasis,
  }) : super(
    key: key,
  );

  
  @override
  Widget build(BuildContext context) {
    return Provide<FontSizeProvide>(
      builder: (context, child, appSize) {
        return Text(data, 
          style: style != null ? Cmstyle(context, style) : TextStyle(
            color: Theme.of(context).primaryColor, 
            fontSize: appSize.textSize + AppSize.defaulSize
          ),
          strutStyle: this.strutStyle,
          textAlign: this.textAlign,
          textDirection: this.textDirection,
          locale: this.locale,
          softWrap: this.softWrap,
          overflow: this.overflow,
          textScaleFactor: this.textScaleFactor,
          maxLines: this.maxLines,
          semanticsLabel: this.semanticsLabel,
          textWidthBasis: this.textWidthBasis,
        );
      }
    );
  }
}

/**
 * Cmstyle主要是为上面的封装服务的
 * fontSize和color属性为有默认值和可根据系统切换
 * 当Cmtext的style属性不传时,则取默认值,默认值为设定好的值, 比如字体大小, APP设置为15, 这里即取值为15
 * color同fontSize
 */
class Cmstyle extends TextStyle {
  Cmstyle(dynamic context, TextStyle style, {
    double fontSize,
    Color color,
    Color backgroundColor,
    FontWeight fontWeight,
    FontStyle fontStyle,
    double letterSpacing,
    double wordSpacing,
    TextBaseline textBaseline,
    double height,
    Locale locale,
    Paint foreground,
    Paint background,
    List<Shadow> shadows,
    List<FontFeature> fontFeatures,
    TextDecoration decoration,
    Color decorationColor,
    TextDecorationStyle decorationStyle,
    double decorationThickness,
    String debugLabel,
    String fontFamily,
    List<String> fontFamilyFallback,
    String package,
  }) : super(
    fontSize: style.fontSize != null ? style.fontSize + Provide.value<FontSizeProvide>(context).textSize : Provide.value<FontSizeProvide>(context).textSize + AppSize.defaulSize,
    color: style.color != null ? style.color : Theme.of(context).primaryColor,
    backgroundColor: style.backgroundColor,
    fontWeight: style.fontWeight,
    fontStyle: style.fontStyle,
    letterSpacing: style.letterSpacing,
    wordSpacing: style.wordSpacing,
    textBaseline: style.textBaseline,
    height: style.height,
    locale: style.locale,
    foreground: style.foreground,
    background: style.background,
    shadows: style.shadows,
    fontFeatures: style.fontFeatures,
    decoration: style.decoration,
    decorationColor: style.decorationColor,
    decorationStyle: style.decorationStyle,
    decorationThickness: style.decorationThickness,
    debugLabel: style.debugLabel,
    fontFamily: style.fontFamily,
    fontFamilyFallback: style.fontFamilyFallback,
  );
}